package com.bupt.liutong.admin.bbs.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bupt.liutong.admin.bbs.bo.BoardBo;
import com.bupt.liutong.admin.bbs.form.BoardForm;
import com.bupt.liutong.admin.core.base.BaseAction;
import com.bupt.liutong.util.BeanUtils;

public class BoardAction extends BaseAction {

	private BoardBo boardBo;

	public ActionForward getBoardsByPage(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		BoardForm boardForm = (BoardForm) form;
		request.setAttribute("yesOrNoMap", ctm.get("yesOrNo"));
		request.setAttribute("boardFormList", boardBo.getBoardsByPage(
				boardForm, request));
		putPageInfo(request, boardForm);
		return mapping.findForward("getBoardsByPageSuccess");
	}

	public ActionForward insertBoard(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("yesOrNoList", getOptionList(ctm.get("yesOrNo"), false));
		request.setAttribute("boardFormList", boardBo.getAllBoards());
		return mapping.findForward("insertBoardSuccess");
	}

	public ActionForward insertBoardDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BoardForm boardForm = (BoardForm) form;
		boardBo.insertBoard(boardForm);
		return mapping.findForward("insertBoardDoSuccess");
	}

	public ActionForward updateBoard(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BoardForm boardForm = (BoardForm) form;
		request.setAttribute("yesOrNoList", getOptionList(ctm.get("yesOrNo"), false));
		request.setAttribute("boardFormList", boardBo.getAllBoards());
		BeanUtils.copyProperties(boardForm, boardBo.getBoardById(boardForm));
		return mapping.findForward("updateBoardSuccess");
	}

	public ActionForward updateBoardDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BoardForm boardForm = (BoardForm) form;
		boardBo.updateBoard(boardForm);
		return mapping.findForward("updateBoardDoSuccess");
	}

	public ActionForward deleteBoards(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("id");
		boardBo.deleteBoards(ids);
		return mapping.findForward("deleteBoardsSuccess");
	}

	public void setBoardBo(BoardBo boardBo) {
		this.boardBo = boardBo;
	}
}
